//
//  ViewController2.swift
//  NewFeaturesDemo
//
//  Created by VEER on 2022/3/8.
//

import UIKit

class ViewController1: UIViewController {
    
    let btnTitles = ["tableView示例", "UIImageResize示例", "新UIButton示例", "UISheetPresentationController示例", "async&await示例"]
    let navColors = [UIColor.red, UIColor.blue, UIColor.white]
    let navTitleColors = [UIColor.red, UIColor.white, UIColor.black]
    let navBarButtonItemColors = [UIColor.red, UIColor.black, UIColor.blue]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "首页"
        view.backgroundColor = UIColor.green
        // Do any additional setup after loading the view.
//        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage.back?.withTintColor(UIColor.black), style: .done, target: self, action: #selector(back))
        
        setupTabAndNav()

        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "左边按钮")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "右边按钮")
        
        let titelLbl = UILabel()
        titelLbl.text = "改变导航栏颜色："
        titelLbl.textColor = UIColor.black
        titelLbl.frame = CGRect(x: 16, y: NavBarHeight + 10, width: 200, height: 20)
        view.addSubview(titelLbl)
        for i in 0..<navColors.count {
            
            let btn = UIButton(type: .custom)
            btn.tag = i
            btn.frame = CGRect(x: 10 + CGFloat(i) * 50.0, y: titelLbl.frame.maxY + 10, width: 50.0, height: 50.0)
            btn.backgroundColor = navColors[i]
            btn.addTarget(self, action: #selector(onChangeNav(sender:)), for: .touchUpInside)
            view.addSubview(btn)
        }
        
        let navTitelLbl = UILabel()
        navTitelLbl.text = "改变导航栏标题颜色："
        navTitelLbl.textColor = UIColor.black
        navTitelLbl.frame = CGRect(x: 16, y: titelLbl.frame.maxY + 70, width: 200, height: 20)
        view.addSubview(navTitelLbl)
        
        for i in 0..<navTitleColors.count {
            
            let btn = UIButton(type: .custom)
            btn.tag = i
            btn.frame = CGRect(x: 10 + CGFloat(i) * 50.0, y: navTitelLbl.frame.maxY + 10, width: 50.0, height: 50.0)
            btn.backgroundColor = navTitleColors[i]
            btn.addTarget(self, action: #selector(onChangeNavTitle(sender:)), for: .touchUpInside)
            view.addSubview(btn)
        }
        
        let leftTitelLbl = UILabel()
        leftTitelLbl.text = "改变左右按钮标题颜色："
        leftTitelLbl.textColor = UIColor.black
        leftTitelLbl.frame = CGRect(x: 16, y: navTitelLbl.frame.maxY + 70, width: 200, height: 20)
        view.addSubview(leftTitelLbl)
        
        for i in 0..<navBarButtonItemColors.count {
            
            let btn = UIButton(type: .custom)
            btn.tag = i
            btn.frame = CGRect(x: 10 + CGFloat(i) * 50.0, y: leftTitelLbl.frame.maxY + 10, width: 50.0, height: 50.0)
            btn.backgroundColor = navBarButtonItemColors[i]
            btn.addTarget(self, action: #selector(onChangeNavButtonItemColor(sender:)), for: .touchUpInside)
            view.addSubview(btn)
        }
        
        
        for i in 0..<btnTitles.count {
            
            let btn = UIButton(type: .custom)
            btn.tag = i
            btn.frame = CGRect(x: (view.bounds.width - 200) * 0.5, y: 400.0 + CGFloat(i) * 50.0, width: 200.0, height: 50.0)
            btn.setTitle(btnTitles[i], for: .normal)
            btn.backgroundColor = UIColor.blue
            btn.addTarget(self, action: #selector(onBtnClick(sender:)), for: .touchUpInside)
            view.addSubview(btn)
        }
        
    }
    
    
    
    func setupNav() {
        
        if #available(iOS 13.0, *) {
            let barApp = UINavigationBarAppearance()
            barApp.backgroundColor = .white
            self.navigationController?.navigationBar.scrollEdgeAppearance = barApp
            self.navigationController?.navigationBar.standardAppearance = barApp
        } else {
            let nav = UINavigationBar.appearance()
            nav.backgroundColor = .white
        }
    }
    
    
    func setupTabAndNav() {
        
        if #available(iOS 13.0, *) {
            // UINavigationBar
            let navigationBarAppearance = UINavigationBarAppearance()
            // 背景色
            navigationBarAppearance.backgroundColor = .white
            // 按钮文字颜色(未发现调整图片颜色的API)
            navigationBarAppearance.buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.black]
            navigationBarAppearance.buttonAppearance.highlighted.titleTextAttributes = [.foregroundColor: UIColor.black]
            navigationBarAppearance.doneButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.black]
            navigationBarAppearance.doneButtonAppearance.highlighted.titleTextAttributes = [.foregroundColor: UIColor.black]
            // 标题文字颜色
            navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.black]
            navigationBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
//            navigationBarAppearance.buttonAppearance
            // 生效
            navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
            navigationController?.navigationBar.standardAppearance = navigationBarAppearance
        } else {
            navigationController?.navigationBar.barTintColor = .red
            navigationController?.toolbar.barTintColor = .blue
            navigationController?.navigationBar.tintColor = .white
            navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
            navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        }
    }
    
    @objc func back() {
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func onBtnClick(sender: UIButton) {
        
        let tilteStr = btnTitles[sender.tag]
        var vc: UIViewController?
        if sender.tag == 0 {
            vc = ViewController3()
        } else if sender.tag == 1 {
            vc = ViewController4()
        }else if sender.tag == 2 {
            vc = ViewController5()
        }else if sender.tag == 3 {
            vc = ViewController6()
        }else if sender.tag == 4 {
            vc = ViewController7()
        }
        vc?.title = tilteStr
        vc?.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc!, animated: true)
    }
    
    @objc func onChangeNav(sender: UIButton) {
        self.navigationBarBgColor = navColors[sender.tag]
//        self.navigationBarColor = navColors[sender.tag]
    }
    
    @objc func onChangeNavTitle(sender: UIButton) {
        self.titleColor = navTitleColors[sender.tag]
    }
    
    @objc func onChangeNavButtonItemColor(sender: UIButton) {
        self.leftBarBtnItemTextColor =  navBarButtonItemColors[sender.tag]
        self.rightBarBtnItemTextColor =  navBarButtonItemColors[sender.tag]
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.leftBarBtnItemTextColor = UIColor.red
    }

}
